﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using BISPOS.Repository;
using System.IO.Ports;

namespace BISPOS
{



    public partial class InvoiceSummaryForm : DevExpress.XtraEditors.XtraForm
    {
        POSForm pOSForm;
        InvoiceRepository invoiceRepository = new InvoiceRepository();
        ProductRepository productRepository = new ProductRepository();
        public InvoiceSummaryForm(POSForm _pOSForm)
        {
            InitializeComponent();
            pOSForm = _pOSForm;
        }


        private void InvoiceSummaryForm_Shown(object sender, EventArgs e)
        {
            totalPriceTextEdit.Text = pOSForm.totalPrice.ToString("C2");
            paidTextEdit.Focus();

        }

        private void paidTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.changesTextEdit.Text = (Convert.ToDecimal(this.paidTextEdit.Text) - pOSForm.totalPrice).ToString("c2");
            }
            catch { this.changesTextEdit.Text = (0).ToString("N2"); }
        }

        private void paidTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (!string.IsNullOrEmpty(paidTextEdit.Text))
                    {
                        if (pOSForm.paymentType.IsCredit)
                        {
                            if (Convert.ToDecimal(paidTextEdit.Text) >= 0)
                            {
                                FinishTransaction();
                            }
                        }
                        else
                        {
                            if ((Convert.ToDecimal(paidTextEdit.Text) - pOSForm.totalPrice) >= 0)
                                FinishTransaction();
                        }

                    }
                }
                catch { }
            }
        }

        public void FinishTransaction()
        {

            try
            {
                SerialPort comPort = new SerialPort("COM1");
                comPort.Open();
                comPort.Write("00000000");
                comPort.Close();
            }
            catch { }
            if (pOSForm.paymentType.IsCredit)
            {
                if (pOSForm.customer != null)
                {
                    invoiceRepository.CreateInvoice(pOSForm.paymentType.Id, pOSForm.employee.Id,
                        pOSForm.customer.Id, 0, "",
                        pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>());
                }
                else
                {
                    invoiceRepository.CreateInvoice(pOSForm.paymentType.Id, pOSForm.employee.Id,
                        null, 0, "",
                        pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>());
                }
            }
            else
            {
                if (pOSForm.customer != null)
                {
                    invoiceRepository.CreateInvoice(pOSForm.paymentType.Id, pOSForm.employee.Id,
                        pOSForm.customer.Id, pOSForm.totalPrice, "",
                        pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>());
                }
                else
                {
                    invoiceRepository.CreateInvoice(pOSForm.paymentType.Id, pOSForm.employee.Id,
                        null, pOSForm.totalPrice, "",
                        pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>());
                }

            }
            Report.Slip slip = new Report.Slip();
            List<POSGridItem> lists = pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>().ToList();
            Invoice invoice = invoiceRepository.LatestInvoice();
   
            slip.DataSource = lists;
            slip.Id.Value = invoice.IdText;
            slip.Sum.Value = invoice.TotalPrice.ToString("N2");
            slip.Paid.Value = paidTextEdit.Text;
            slip.Change.Value = changesTextEdit.Text;
            slip.hDate.Value = DateTime.Now.ToShortDateString();
            slip.hTime.Value = DateTime.Now.ToShortTimeString();
            slip.hCustomer.Value = pOSForm.customerTextEdit.Text;
            slip.hPaymentType.Value = pOSForm.paymentTypeTextEdit.Text;


            slip.RequestParameters = false;
            slip.Print();


            pOSForm.ResetForm();


            

            //Print


           /* string[] line = new string[100];
            line[0] = "สหกรณ์การเกษตรลับแล";
            line[1] = "เลขที่: " + invoiceRepository.LatestInvoiceNumber();
            int count = 2;
            if (pOSForm.customer != null)
            {
                line[count] = ("สมาชิก: " + pOSForm.customer.Name); count++;
                line[count] = ("ยอดซื้อสะสม: " + pOSForm.customer.Invoices.Where(x => x.IsActive).Sum(x => x.TotalPrice).ToString("N2")); count++;
            }
            line[count] = "รายการขาย"; count++;
            IEnumerable<POSGridItem> items = pOSForm.pOSGridItemBindingSource.OfType<POSGridItem>();
            foreach (POSGridItem item in items)
            {
                string name = item.ProductName;
                if (item.Quantity > 1)
                {
                    name = name + " (x" + item.Quantity.ToString("n2") + ")";
                }
                name = name + "\t" + item.TotalPrice.ToString("N2");
                line[count]=(name);count++;
            }

            line[count] = ("ราคารวม: " + items.Sum(x => x.TotalPrice).ToString("N2"));count++;
            line[count]=("รับเงิน: " + Convert.ToDecimal(paidTextEdit.Text).ToString("N2"));count++;
            line[count]=("ทอน: " + Convert.ToDecimal(changesTextEdit.Text).ToString("N2"));count++;
            line[count] = ("ขอบคุณที่ใช้บริการ " + Convert.ToDecimal(changesTextEdit.Text).ToString("N2")); count++;
            

            PrintTextDocument print = new PrintTextDocument(line, new Font(FontFamily.GenericSansSerif, 4));
            print.Print();*/
            //End Print

            this.Close();
        }
    }
}